﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace mesoBoard.Services
{
    public static class SiteConfig
    {
        public static Config BoardName { get { return GetConfig("BoardName"); } }
        public static Config BoardURL { get { return GetConfig("BoardURL"); } }
        public static Config AutomatedFromEmail { get { return GetConfig("AutomatedFromEmail"); } }
        public static Config ContactUsToEmail { get { return GetConfig("ContactUsToEmail"); } }
        public static Config PageTitlePhrase { get { return GetConfig("PageTitlePhrase"); } }
        public static Config UsernameMax { get { return GetConfig("UsernameMax"); } }
        public static Config UsernameMin { get { return GetConfig("UsernameMin"); } }
        public static Config PasswordMin { get { return GetConfig("PasswordMin"); } }
        public static Config PasswordMax { get { return GetConfig("PasswordMax"); } }
        public static Config Offline { get { return GetConfig("Offline"); } }
        public static Config DateFormat { get { return GetConfig("DateFormat"); } }
        public static Config TimeLimitToEditPost { get { return GetConfig("TimeLimitToEditPost"); } }
        public static Config PostsPerPage { get { return GetConfig("PostsPerPage"); } }
        public static Config ThreadsPerPage { get { return GetConfig("ThreadsPerPage"); } }
        public static Config AvatarFileSize { get { return GetConfig("AvatarFileSize"); } }
        public static Config AvatarWidth { get { return GetConfig("AvatarWidth"); } }
        public static Config AvatarHeight { get { return GetConfig("AvatarHeight"); } }
        public static Config BoardCreateDate { get { return GetConfig("BoardCreateDate"); } }
        public static Config MaxFileSize { get { return GetConfig("MaxFileSize"); } }
        public static Config Language { get { return GetConfig("Language"); } }
        public static Config AccountActivation { get { return GetConfig("AccountActivation"); } }
        public static Config BoardTheme { get { return GetConfig("BoardTheme"); } }
        public static Config OverrideUserTheme { get { return GetConfig("OverrideUserTheme"); } }
        public static Config TimeBetweenPosts { get { return GetConfig("TimeBetweenPosts"); } }
        public static Config AllowThreadImage { get { return GetConfig("AllowThreadImage"); } }
        public static Config TimeOffset { get { return GetConfig("TimeOffset"); } }
        public static Config SyntaxHighlighting { get { return GetConfig("SyntaxHighlighting"); } }
        public static Config CaptchaFontColor { get { return GetConfig("CaptchaFontColor"); } }
        public static Config CaptchaBackgroundColor { get { return GetConfig("CaptchaBackgroundColor"); } }
        public static Config CaptchaWidth { get { return GetConfig("CaptchaWidth"); } }
        public static Config CaptchaHeight { get { return GetConfig("CaptchaHeight"); } }
        public static Config CaptchaFontFamily { get { return GetConfig("CaptchaFontFamily"); } }
        public static Config CaptchaWarpFactor { get { return GetConfig("CaptchaWarpFactor"); } }

        private static Config GetConfig(string configName)
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/ConfigCache.xml")))
                UpdateCache();

            XElement file = XElement.Load(HttpContext.Current.Server.MapPath("~/ConfigCache.xml"));
            XElement found = file.Element(configName);

            return new Config
            { 
                Name = configName,
                Value = found.Attribute("Value").Value,
                Group = found.Attribute("Group").Value,
                Options = found.Attribute("Options").Value,
                Note = found.Attribute("Note").Value,
                Type = found.Attribute("Type").Value
            };
        }

        public static void UpdateCache()
        {
            ConfigRepository Configs = new ConfigRepository(new mbEntities());
            IEnumerable<Config> Allof = Configs.Configs();

            XElement file = new XElement("mesoBoardConfigs");
            foreach (Config c in Allof)
            {
                XElement v = new XElement(c.Name,
                    new XAttribute("Value", string.IsNullOrEmpty(c.Value) ? "" : c.Value),
                    new XAttribute("Type", string.IsNullOrEmpty(c.Type) ? "" : c.Type),
                    new XAttribute("Group", string.IsNullOrEmpty(c.Group) ? "" : c.Group),
                    new XAttribute("Options", string.IsNullOrEmpty(c.Options) ? "" : c.Options),
                    new XAttribute("Note", string.IsNullOrEmpty(c.Note) ? "" : c.Note));

                file.Add(v);
            }
            file.Save(HttpContext.Current.Server.MapPath(Path.Combine("~/", "ConfigCache.xml")));
        }
    }
}